package oiunt.hello;

import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.GestureDetector.OnGestureListener;

public class HorizontalScroll extends Activity implements OnGestureListener {
	private static final int X_MAX = 800;
	private static final int Y_MAX = 600;
	private int scrollX = 0;
	private int scrollY = 0;

	MyView main;
	Bitmap bmp;
	Bitmap adapt;
	Resources res;
	Paint paint;
	GestureDetector gestureScanner;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		gestureScanner = new GestureDetector(this);
		paint = new Paint();

		res = getResources();
		bmp = BitmapFactory.decodeResource(res, R.drawable.front);
		adapt = Bitmap.createBitmap(bmp);

		main = new MyView(this);
		setContentView(main, new ViewGroup.LayoutParams(1200, 1800));
	}

	@Override
	public boolean onTouchEvent(MotionEvent me) {
		return gestureScanner.onTouchEvent(me);
	}

//	@Override
	public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
			float distanceY) {
		main.handleScroll(distanceX, distanceY);
		return true;
	}

	// //////////////////
	// /////////////////
	// ////////////////
//	@Override
//	public boolean onDown(MotionEvent e) {
//		return true;
//	}
//
//	@Override
//	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
//			float velocityY) {
//		return true;
//	}
//
//	@Override
//	public void onLongPress(MotionEvent e) {
//	}
//
//	@Override
//	public void onShowPress(MotionEvent e) {
//	}
//
//	@Override
//	public boolean onSingleTapUp(MotionEvent e) {
//		return true;
//	}

	// //////////////////
	// /////////////////
	// ////////////////

	class MyView extends View {
		public MyView(Context context) {
			super(context);
		}

		@Override
		protected void onDraw(Canvas canvas) {
			canvas.drawBitmap(adapt, 0, 0, paint);
		}

		public void handleScroll(float distX, float distY) {
			// X-Axis ////////////////////////////////

			if (distX > 6.0) {
				if (scrollX < 460) {
					scrollX += 15;
				}
			} else if (distX < -6.0) {
				if (scrollX >= 15) {
					scrollX -= 15;
				}
			}
			// //////////////////////////////////////////

			// Y-AXIS //////////////////////////////////
			if (distY > 6.0) {
				if (scrollY < 100) {
					scrollY += 15;
				}
			} else if (distY < -6.0) {
				if (scrollY >= 15) {
					scrollY -= 15;
				}
			}
			// //////////////////////////////////////////

			if ((scrollX <= 480) && (scrollY <= 120)) {
				adapt = Bitmap.createBitmap(bmp, scrollX, scrollY, 320, 480);
				invalidate();
			}
		}
	}

	public boolean onDown(MotionEvent arg0) {
		// TODO Auto-generated method stub
		return false;
	}

	public boolean onFling(MotionEvent arg0, MotionEvent arg1, float arg2,
			float arg3) {
		// TODO Auto-generated method stub
		return false;
	}

	public void onLongPress(MotionEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	public void onShowPress(MotionEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	public boolean onSingleTapUp(MotionEvent arg0) {
		// TODO Auto-generated method stub
		return false;
	}
}
